package com.ruoyi.source.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.SourceDataCountInfo;
import com.ruoyi.db.mapper.SourceDataCountInfoMapper;
import com.ruoyi.source.service.ISourceDataCountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.PageUtils.startPage;


/**
 * 线索数据统计Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceDataCountInfoServiceImpl implements ISourceDataCountInfoService {
    @Autowired
    private SourceDataCountInfoMapper sourceDataCountInfoMapper;

    /**
     * 查询线索数据统计
     *
     * @param id 线索数据统计主键
     * @return 线索数据统计
     */
    @Override
    public SourceDataCountInfo selectSourceDataCountInfoById(Long id) {
        return sourceDataCountInfoMapper.selectSourceDataCountInfoById(id);
    }

    /**
     * 查询线索数据统计列表
     *
     * @param sourceDataCountInfo 线索数据统计
     * @return 线索数据统计
     */
    @Override
    public List<SourceDataCountInfo> selectSourceDataCountInfoList(SourceDataCountInfo sourceDataCountInfo) {
        return sourceDataCountInfoMapper.selectSourceDataCountInfoList(sourceDataCountInfo);
    }

    /**
     * 查询线索数据统计列表
     *
     * @param sourceDataCountInfo 线索数据统计
     * @return 线索数据统计
     */
    @Override
    public List<Map<String, Object>> selectSourceDataCountInfoMapList(SourceDataCountInfo sourceDataCountInfo) {
        return sourceDataCountInfoMapper.selectSourceDataCountInfoMapList(sourceDataCountInfo);
    }


    /**
     * 新增线索数据统计
     *
     * @param sourceDataCountInfo 线索数据统计
     * @return 结果
     */
    @Override
    public int insertSourceDataCountInfo(SourceDataCountInfo sourceDataCountInfo) {
        sourceDataCountInfo.setCreateTime(DateUtils.getNowDate());
        return sourceDataCountInfoMapper.insertSourceDataCountInfo(sourceDataCountInfo);
    }

    /**
     * 修改线索数据统计
     *
     * @param sourceDataCountInfo 线索数据统计
     * @return 结果
     */
    @Override
    public int updateSourceDataCountInfo(SourceDataCountInfo sourceDataCountInfo) {
        sourceDataCountInfo.setUpdateTime(DateUtils.getNowDate());
        return sourceDataCountInfoMapper.updateSourceDataCountInfo(sourceDataCountInfo);
    }

    /**
     * 批量删除线索数据统计
     *
     * @param ids 需要删除的线索数据统计主键
     * @return 结果
     */
    @Override
    public int deleteSourceDataCountInfoByIds(Long[] ids) {
        return sourceDataCountInfoMapper.deleteSourceDataCountInfoByIds(ids);
    }

    /**
     * 删除线索数据统计信息
     *
     * @param id 线索数据统计主键
     * @return 结果
     */
    @Override
    public int deleteSourceDataCountInfoById(Long id) {
        return sourceDataCountInfoMapper.deleteSourceDataCountInfoById(id);
    }

    @Override
    public Map<String, Object> selectSourceAllDataCountInfoMapList2(SourceDataCountInfo sourceDataCountInfo) {
        return sourceDataCountInfoMapper.selectSourceAllDataCountInfoMapList2(sourceDataCountInfo);
    }

    @Override
    public List<Map<String, Object>> getcost(Integer type) {

        LocalDate dateInfo = LocalDate.now();

        if (type == 0 || type == 1) {
            dateInfo = (type == 0) ? dateInfo : dateInfo.minusDays(1); // 今天或者昨天的日期
        } else if (type == 2 || type == 3) {

            int date = (type == 2) ? 3 : 5; // 根据类型确定要减去的天数
            LocalDate startDate = dateInfo.minusDays(date); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1); // 结束日期
            List<Map<String, Object>> maplist = sourceDataCountInfoMapper.getcostDate(startDate.toString(), endDate.toString());
            return maplist;
        } else if (type == 4) {
            LocalDate startDate = LocalDate.now().minusDays(30); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1); // 结束日期

            List<Map<String, Object>> maplist = sourceDataCountInfoMapper.getcostDate(startDate.toString(), endDate.toString());
            return maplist;
        }

        List<Map<String, Object>> maplist = sourceDataCountInfoMapper.getcost(dateInfo.toString());


        return maplist;
    }
    @Override
    public List<Map<String, Object>> getCpaCost(SourceDataCountInfo sourceDataCountInfo) {
        return sourceDataCountInfoMapper.getCpaCost(sourceDataCountInfo);
    }
}
